import { defineStore } from 'pinia'
import { ref } from 'vue'
import {userGetFollowService} from "@/api/user.js";
import {
  addPostCollectService,
  addPostLikeService,
  delPostCollectService,
  delPostLikeService,
  getPostCollectService,
  getPostLikesService
} from "@/api/postInfo.js";
import {addFollowService, delFollowService, getFollowService} from "@/api/userInfo.js";

// 用户模块
export const useUserInfoStore = defineStore(
  'user-info',
  () => {
    const follow = ref([])
    const postLike = ref([])
    const postCollect = ref([])

    const getFollow = async () => {
      const res = await getFollowService()
      follow.value = res.data.data
    }
    const getPostLike= async () => {
      const res = await getPostLikesService()
      postLike.value = res.data.data
    }
    const getPostCollect = async () => {
      const res = await getPostCollectService()
      postCollect.value = res.data.data
    }

    const removePostLike = async (id) => {
      postLike.value = postLike.value.filter(item => item !== id)
      await delPostLikeService(id)
    }
    const removeFollow = async (id) => {
      await delFollowService(id)
      follow.value = follow.value.filter(item => item !== id)
    }
    const removePostCollect = async (id) => {
      postCollect.value = postCollect.value.filter(item => item !== id)
      await delPostCollectService(id)
    }

    const addPostLike = async (id) => {
      await addPostLikeService(id)
      postLike.value.push(id)
    }
    const addPostCollect = async (id) => {
      await addPostCollectService(id)
      postCollect.value.push(id)
    }
    const addFollow= async (id) => {
      await addFollowService(id)
      follow.value.push(id)
    }

    const clear= () => {
      postCollect.value = []
      postLike.value = []
      follow.value = []
    }

    return { follow,postLike,postCollect ,getFollow,getPostLike,getPostCollect,
            removeFollow,removePostCollect,removePostLike,
            addFollow,addPostLike,addPostCollect,
            clear
    }
  },
  {
    persist: true // 持久化
  }
)

export default useUserInfoStore
